package com.jackrain.clouddata.oms.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@Component
public class RetSaleApplyDao {
    private static final Logger logger = LoggerFactory.getLogger(RetSaleApplyDao.class);

    @Transactional
    public void update(JSONObject paramObject,String tag,long ad_client_id) throws SQLException {


        logger.info("==================调用dao=================");
        /* paramObject格式
         *{
         * "name":"xxx",
         * "value":"xxx",
         * "item":[{"name":"xxx","value":[{xxx},{xxx}]}]
         * }
         */
        String tableName = paramObject.getString("name");
        JSONObject valueJson = paramObject.getJSONObject("value");

        List paramList = new ArrayList();
        Set<String> keySet = valueJson.keySet();
        StringBuffer strBuf = new StringBuffer("UPDATE "+tableName+" SET ");
        paramList = new ArrayList();
        for (String key: keySet) {
            strBuf.append(key+" = ? ,");
            if (key.substring(key.length()-4,key.length()).equals("DATE")) {
                paramList.add(new Date(valueJson.getLong(key)));
            }else if (null == valueJson.get(key) || "".equals(valueJson.get(key))) {
                paramList.add("");
            }else{
                paramList.add(valueJson.get(key));
            }
        }
        strBuf = new StringBuffer(strBuf.substring(0,strBuf.length()-1));
        strBuf.append(" WHERE BILLNO = ? AND AD_CLIENT_ID = ?");
        paramList.add(valueJson.get("BILLNO"));
        paramList.add(ad_client_id);
        QueryEngine.getInstance().executeUpdate(strBuf.toString(),paramList.toArray());

        JSONArray items = paramObject.getJSONArray("item");
        for (int i = 0; i < items.size(); i++) {
            String itemTableName = items.getJSONObject(i).getString("name");
            JSONArray itemValues = items.getJSONObject(i).getJSONArray("value");
            // 插入子表
            for (int j = 0; j < itemValues.size(); j++) {
                valueJson = itemValues.getJSONObject(j);
                keySet = valueJson.keySet();
                strBuf = new StringBuffer("UPDATE "+itemTableName+" SET ");
                paramList = new ArrayList();
                for (String key: keySet) {
                    strBuf.append(key+" = ? ,");
                    if (key.substring(key.length()-4,key.length()).equals("DATE")) {
                        paramList.add(new Date(valueJson.getLong(key)));
                    }else if (null == valueJson.get(key) || "".equals(valueJson.get(key))) {
                        paramList.add("");
                    }else{
                        paramList.add(valueJson.get(key));
                    }
                }
                strBuf = new StringBuffer(strBuf.substring(0,strBuf.length()-1));
                strBuf.append(" WHERE BILLNO = ? AND AD_CLIENT_ID = ?");
                paramList.add(valueJson.get("BILLNO"));
                paramList.add(ad_client_id);
                QueryEngine.getInstance().executeUpdate(strBuf.toString(),paramList.toArray());

            }
        }
    }
}
